﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using personalplaner.business;
using personalplaner.business.dienstplan;
using personalplaner.business.dienstplan.plancollection;
using personalplaner.business.enums;
using personalplaner.business.extensions;
using personalplaner.business.stamm;
using personalplaner.common.mvvm;
using personalplaner.common.mvvm.commands;
using personalplaner.servicemodel.dataaccess;

namespace personalplaner.plugins.plananalysis.viewmodels
{
	public class YearOverviewViewModel : BaseViewModel
	{
		public YearOverviewViewModel(DependencyObject parent)
            : base(parent)
        {
        }

		#region Commands

		ICommand _printcommand;
		public ICommand PrintCommand
		{
			get
			{
				if (_printcommand == null)
					_printcommand = new RelayCommand(OnPrintCommand, parameter => IsYearPlaned);
				return _printcommand;
			}
		}

		private void OnPrintCommand(object parameter)
		{
			var parent = Parent as YearOverview;
			if (parent == null)
				return;


			//var grid = new Grid();
			//grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
			//grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
			//grid.RowDefinitions.Add(new RowDefinition());

			//var tb = new TextBlock{
			//    Text = 


			var el = parent.Grids.RenderNewElement();
			((FrameworkElement)el).HorizontalAlignment = HorizontalAlignment.Left;
			((FrameworkElement)el).VerticalAlignment = VerticalAlignment.Top;
			((FrameworkElement)el).Margin = new Thickness(40, 60, 40, 60);

			var pc = new PrintView();
			pc.Pages = new List<UIElement> { el };

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = true;

			var service = GetService<IMainWindowDisplayContainer>();
			service.AddSimpleContentItem(enDisplayItemType.Custom, pc, true);

		}

		#endregion

		#region Properties

		IList<PlanYear> _years;
		public IList<PlanYear> Years
		{
			get
			{
				if (_years == null)
				{
					var da = GetService<ICommonDataAccess>();
					var lst = da.GetActiveYears();
					_years = lst != null ? new ObservableCollection<PlanYear>(lst) : new ObservableCollection<PlanYear>();

					SelectedYear = (from a in _years where a.Status == business.enums.enStatus.Active select a).FirstOrDefault();
				}
				return _years;
			}
		}

		PlanYear _selectedYear;
		public PlanYear SelectedYear
		{
			get
			{
				return _selectedYear;
			}
			set
			{
				_selectedYear = value;
				RaisePropertyChanged("SelectedYear");

				SetData();
			}
		}

		int _prsId;
		public int PrsId
		{
			get { return _prsId; }
			set
			{
				_prsId = value;
				RaisePropertyChanged("PrsId");

				SetData();
			}
		}

		ObservableCollection<IDienstPlanRow> _collection;
		public ObservableCollection<IDienstPlanRow> Collection
		{
			get
			{
				if (_collection == null)
					_collection = new ObservableCollection<IDienstPlanRow>();
				return _collection;
			}
		}

		public bool IsYearPlaned
		{
			get
			{
				return Collection.Count > 0;
			}
		}

		static Color? HolidayColor { get; set; }

		static Color? SaturdayColor { get; set; }

		static Color? SundayColor { get; set; }

		#endregion

		#region Implementation

		private void SetData()
		{
			if (PrsId <= 0 || SelectedYear == null)
				return;

			var da = GetService<IPlanDataAccess>();
			var options = GetService<IOptionsDataAccess>();
			bool colorweekend = options.ColorWeekendInPlan;

			HolidayColor = options.HolidayColor;
			SaturdayColor = options.SaturdayColor;
			SundayColor = options.SundayColor;

			var headers = da.GetPlanOverviewHeaders(PrsId, SelectedYear.JahrId);

			Collection.Clear();

			if (headers == null)
			{
				RaisePropertyChanged("Collection");
				RaisePropertyChanged("IsYearPlaned");
				return;
			}

			foreach (var header in headers)
			{
				var row = new OverviewRow
				{
					ID = header.PlanId,
					Name = header.PlanName,
					AbteilungId = header.AbteilungId,
					Abteilung = header.AbteilungName,

					Soll = header.Soll,
					Haben = header.Haben,
					Anpassung = header.Anpassung,
					Ferien = header.Ferien
				};

				var lst = da.GetPlanRow(header.PlanId, header.PrsSchemaId, PrsId).ToList();
                row.Initialize(header.Von, lst);

				if (colorweekend)
					SetWeekendColor(row, header.PlanId);

				Collection.Add(row);
			}

            RaisePropertyChanged("Collection");
			RaisePropertyChanged("IsYearPlaned");
		}

		private void SetWeekendColor(IEnumerable<PlanItem> items, int planid)
		{
			foreach (var item in items)
			{
				//if (item.Color != Colors.Transparent && item.Color != Colors.White)
				if (item.Color != null && !((Color)item.Color).IsEmptyColor())
					continue;

				if (item.Datum.DayOfWeek == DayOfWeek.Saturday)
				{
					item.Color = SaturdayColor;
				}
				else if (item.Datum.DayOfWeek == DayOfWeek.Sunday)
				{
					item.Color = SundayColor;
				}

				if (IsDayFeiertag(item.Datum, planid))
				{
					item.Color = SundayColor;
				}
			}
		}

		private bool IsDayFeiertag(DateTime dateTime, int planid)
		{
			var da = GetService<IPlanDataAccess>();
			var lst = da.GetFeiertageInPlan(planid);
			if (lst == null)
				return false;

			var itms = from a in lst where a.Von == dateTime select a;

			return itms.Count() > 0;
		}

		#endregion

	}
}
